import CryptoJS from "crypto-js";

export function encryptWithAES(message, key, iv) {
  key = CryptoJS.enc.Utf8.parse(key.toString("hex"));
  iv = CryptoJS.enc.Utf8.parse(iv.toString("hex"));
  const encrypted = CryptoJS.AES.encrypt(message, key, {
    iv,
    mode: CryptoJS.mode.CBC, // 加密模式，这里使用CBC模式
    padding: CryptoJS.pad.Pkcs7, // 填充方式
  });
  return encrypted.toString();
}

export function decryptWithAES(ciphertext, key, iv) {
  key = CryptoJS.enc.Utf8.parse(key.toString("hex"));
  iv = CryptoJS.enc.Utf8.parse(iv.toString("hex"));
  const decrypted = CryptoJS.AES.decrypt(ciphertext, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
}
export function encryptAESDefault(message) {
  return encryptWithAES(message, defaultKey, defaultIv);
}
export function decryptAESDefault(ciphertext) {
  return decryptWithAES(ciphertext, defaultKey, defaultIv);
}
const defaultKey = "57711c17bd303059f906ffb148e0503c";
const defaultIv = "9be125ed7bc04102d2a106a35b347edb";
